<?php 
    session_start();
    if (!isset($_SESSION['username'])) {
        header('location:/setup/');
    }
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图库管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css" />
    <style>.layui-upload-img{width: 100px; height: 100px; margin: 0 10px 10px 0;}</style>
</head>

<body style='padding:10px'>
    <button type="button" class="layui-hide" id="bt_upload_img"></button>
    <div class="layui-fluid">
        <div id="fileManager" lay-filter="file_filter"></div>
    </div>
</body>
<script src="../layui/layui.js"></script>
<script>
    layui.extend({'fileManager': 'ext/fileManager'});
    layui.use(['fileManager', 'layer', 'upload'], function () {
        var fileManager = layui.fileManager
            , $ = layui.$
            , upload = layui.upload
            , layer = layui.layer;
        $('title').html($('title').html() + ' version:' + fileManager.v);
        var upIns = upload.render({
            elem: '#bt_upload_img' //绑定元素
            , url: 'data.php?action=upload' //上传接口
            , field: 'file[]'
        })
       
        fileManager.render({
            elem: '#fileManager'
            , method: 'post'
            , btn_upload: true
            , btn_create: false
            , url: 'data.php?action=get_file_data'
            , thumb: {'nopic': '/filemanage/upload/null-100x100.jpg', 'width': 100, 'height': 100}
            , parseData: function (res) {
                let _res = [];
                _res.code = 0;
                _res.data = res.images;
                _res.count = res.count
                return _res;
            }
            , page: {limit: 15}
    
        });
        //监听图片选择事件
        fileManager.on('pic(file_filter)', function (obj) {
            var data = obj.data;
            var index = parent.layer.getFrameIndex(window.name); 
            parent.window.callback && parent.window.callback(obj.data);
            parent.layer.close(index);
        });

            //监听图片上传事件
            fileManager.on('uploadfile(file_filter)', function (obj) {
            //obj.obj 当前对象
            //obj.path 路径
            //更改上传组件参数
            upIns.config.data = {'path': obj.path};
            upIns.config.done = function (res) {
                fileManager.reload('fileManager');
            }
            var e = document.createEvent("MouseEvents");
            e.initEvent("click", true, true);
            document.getElementById("bt_upload_img").dispatchEvent(e)
        });
        
    });
</script>

</html>